package com.clever.mall.service.impl;

import com.clever.mall.commons.dto.SearchRequestParam;
import com.clever.mall.entity.Search;
import com.clever.mall.mapper.SearchMapper;
import com.clever.mall.service.SearchService;
import com.clever.mall.utils.MyBeanUtils;
import com.clever.mall.vo.SearchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {
    @Resource
    SearchMapper searchMapper;

    //查找所有用户的搜索数高的前8条
    @Override
    public List<SearchVo> findAllSearch() {
        List<Search> searches = searchMapper.selectAll();
        return MyBeanUtils.copyListProperties(searches,SearchVo::new);
    }

    //超找用户的搜索关键字前8条
    @Override
    public List<SearchVo> findByUserId(int userId) {
        List<Search> searches = searchMapper.selectByUserId(userId);
        return MyBeanUtils.copyListProperties(searches,SearchVo::new);
    }

    //删除用户的搜索记录
    @Override
    public int deleteByUserId(int userId) {
        int i = searchMapper.updateByUserId(userId);
        return i;
    }

    //增加搜索关键字
    @Override
    public int addWords(SearchRequestParam requestParam) {
        int i = searchMapper.addWords(requestParam);
        return i;
    }

    //判断关键字是否已存在
    @Override
    public Search findByWord(String word) {
        Search search = searchMapper.selectByWord(word);
        return search;
    }

    //更改关键字搜索次数
    @Override
    public int modifyCount(Search search) {
        int i = searchMapper.updateSearchCount(search);
        return i;
    }
}
